$(document).ready(function () {

    if ($('.tree-drag-drop-override').length == 1) {
        PrimeFaces.widget.VerticalTree.prototype.makeDraggable = function (b) {
            var c = this,
                a = this.id;
            b.draggable({
                helper: function () {
                    var d = $('<div class="ui-tree-draghelper ui-state-highlight"></div>');
                    d.width(c.jq.width());
                    d.height(20);
                    return d
                },
                start: function () {
                    var cs = $('span.ui-treenode-label.ui-state-hover'),
                        ps = cs.closest("li.ui-treenode"),
                        nss = ps.siblings();

                    var g = ps.closest("li");
                    c.collapseNode(g);

                    nss.each(function () {
                        var t = $(this);
                        var g = t.closest("li");
                        c.collapseNode(g);
                    });


                    ps.parents().siblings().children("span").addClass('ui-state-disabled');
                },
                stop: function () {
                    $('.ui-tree-container').find("li.ui-treenode span").removeClass('ui-state-disabled');

                },
                appendTo: document.body,
                zIndex: ++PrimeFaces.zindex,
                revert: true,
                scope: a
            }).data({
                dragsourceid: this.jqId,
                dragmode: this.cfg.dragMode
            })
        };

        PrimeFaces.widget.VerticalTree.prototype.makeDropPoints = function (b) {
            var c = this,
                a = this.id;
            b.droppable({
                hoverClass: "ui-state-hover",
                accept: "span.ui-treenode-content",
                tolerance: "pointer",
                scope: a,
                drop: function (e, p) {
                    var f = $(p.draggable.data("dragsourceid")).data("widget"),
                        n = p.draggable.data("dragmode"),
                        m = c,
                        r = $(this),
                        q = r.closest("li.ui-treenode-parent"),
                        s = c.getRowKey(q),
                        i = p.draggable.closest("li.ui-treenode"),
                        g = c.findTargetDragNode(i, n),
                        k = c.getRowKey(g),
                        l = i.children("span.ui-treenode-content"),
                        o = l.children("span.ui-treenode-label"),
                        d = g.next("li.ui-tree-droppoint"),
                        t = g.parent().closest("li.ui-treenode-parent"),
                        h = (f.id !== m.id);
                    p.helper.remove();
                    r.removeClass("ui-state-hover");
                    var j = c.validateDropPoint(i, r);
                    if (!j) {
                        return
                    }
                    g.hide().insertAfter(r);
                    if (h) {
                        if (f.isCheckboxSelection()) {
                            f.unselectSubtree(g)
                        }
                        o.removeClass("ui-state-highlight");
                        d.remove();
                        c.updateDragDropBindings(g)
                    } else {
                        d.insertAfter(g)
                    }
                    if (t.length && (t.find("> ul.ui-treenode-children > li.ui-treenode").length === 0)) {
                        c.makeLeaf(t)
                    }
                    g.fadeIn();
                    if (c.isCheckboxSelection()) {
                        c.syncDNDCheckboxes(f, t, q)
                    }
                    c.syncDragDrop();
                    if (h) {
                        f.syncDragDrop()
                    }
                    c.fireDragDropEvent({
                        dragNodeKey: k,
                        dropNodeKey: s,
                        dragSource: f.id,
                        dndIndex: r.prevAll("li.ui-treenode").length,
                        transfer: h
                    })
                }
            })
        };
        PrimeFaces.widget.VerticalTree.prototype.makeDropNodes = function (b) {
            return false;
        };
    }
});

TreeCustomBehavior = {
    onTransferOn: function () {
        $(".free-position .ui-treenode-label").addClass("free-positions");
        this.enableTrackTooltip(true);
    },

    onTransferOff: function () {
        $(".free-position .ui-treenode-label").removeClass("free-positions");
        this.enableTrackTooltip(false);
    },

    enableTrackTooltip: function (enabled) {
        var tooltip = $('.track_tooltip');
        if (enabled) {
            $('.ui-layout-west').mousemove(function (event) {
                tooltip.css('display', 'block');
                tooltip.css('top', event.pageY );
                tooltip.css('left', event.pageX + 15);
            });
        } else {
            $('#west_panel').off('mousemove');
            tooltip.css('display', 'none');
        }
    }
};
